package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.bean.ShoppingCart;
import com.itheima.common.BaseContext;
import com.itheima.dao.ShoppingCartDao;
import com.itheima.exception.CustomException;
import com.itheima.service.ShoppingCartService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author Tang
 * @date 2023/2/25
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartDao, ShoppingCart> implements ShoppingCartService {

    @Resource
    private ShoppingCartDao shoppingCartDao;

    @Override
    public void addCart(ShoppingCart shoppingCart) {
        //0.获取当前用户id
        Long userId = BaseContext.getCurrentId();
        if (userId == null) {
            throw new CustomException("请先登录!");
        }

        //1.查询购物车表是否已存在需要添加的商品  根据userId、菜品id（可能口味）或者套餐id 唯一确定一条数据
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);
        if (shoppingCart.getDishId() != null) { //添加菜品  eg: 添加一个 辣子鸡 中辣，添加 辣子鸡  不辣  添加米饭
            wrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
            wrapper.eq(shoppingCart.getDishFlavor() != null, ShoppingCart::getDishFlavor, shoppingCart.getDishFlavor());
        } else {                                //添加套餐
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart cart = this.getOne(wrapper);

        //2.如果是新的商品，设置number数量为1，
        if (cart == null) {
            cart = shoppingCart;
            //3.补齐字段：user_id、create_time
            cart.setNumber(1);
            cart.setUserId(userId);
            cart.setCreateTime(LocalDateTime.now());
        } else {
            //4.如果不是新的商品，设置number数量+1
            cart.setNumber(cart.getNumber() + 1);
        }

        //6.保存到购物车表中  【注意】需要调用更新或者新增方法
        this.saveOrUpdate(cart);
    }

    /**
     * 减少购物车中菜品或套餐 task07
     * @param shoppingCart
     * @return
     */
    @Override
    @Transactional
    public int sub(ShoppingCart shoppingCart) {

        Long dishId = shoppingCart.getDishId(); //拿到需要减少的菜品ID



        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());  //选取当前用户的购物车


        if (dishId != null){  //当前减少的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId)
                    .eq(shoppingCart.getDishFlavor() != null,ShoppingCart::getDishFlavor,shoppingCart.getDishFlavor());
        }else {  //当前减少的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart shoppingCartOne = shoppingCartDao.selectOne(queryWrapper);  //搜索满足条件的购物车

        Integer number = shoppingCartOne.getNumber();  //菜品数量

        if (number <= 1){
            shoppingCartDao.delete(queryWrapper);
        }

        //当前菜品或套餐已经存在购物车 ,减一
        number -= 1;
        shoppingCartOne.setNumber(number);
        shoppingCartDao.updateById(shoppingCartOne);


        return 1;

    }
}
